package com.baiyun.yunpan.controller;

import com.baiyun.yunpan.entity.Share;
import com.baiyun.yunpan.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @author Sam
 * @date 2021/5/25 12:06
 */
@RestController
@RequestMapping("share")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ShareController {
    @Autowired
    private ShareService shareService;

    @GetMapping("/addShare")
    public int addShare(Share share){

        Date date = new Date();
        share.setExpiredate(date.getTime());
        shareService.addShare(share);
        return shareService.getId();
    }

    @GetMapping("/getShare")
    public Object getShare(int shareid){
        Share share = shareService.getShare(shareid);

        if (share==null){
            return "分享已过期或未存在该分享";
        }
        Date date = new Date();
        if ((date.getTime()-share.getExpiredate())>(7*24*3600*1000)){
            shareService.delete(share.getShareid());
            return "分享已过期";
        }
        return share;
    }




}
